package com.copa.l226;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author copa
 * @createDate 2022-08-05 09:36
 * @function 递归法（这个我是没想到的...）
 */
public class Test2 {

    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode left = invertTree(root.left);
        TreeNode right = invertTree(root.right);
        root.left = right;
        root.right = left;
        return root;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(4);
        root.left = new TreeNode(2);
        root.right = new TreeNode(7);
        root.left.left = new TreeNode(1);
        root.left.right = new TreeNode(3);
        root.right.left = new TreeNode(6);
        root.right.right = new TreeNode(9);
        TreeNode result = new Test2().invertTree(root);
        if (result == null) {
            System.out.println("没有树节点");
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(result);
        while (queue.size() > 0) {
            int size = queue.size();
            while (size > 0) {
                TreeNode node = queue.poll();
                System.out.print(node.val + "\t");
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
                size--;
            }
        }
    }
}
